package com.pets.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pets.dao.AdminMapper;
import com.pets.entity.Admin;
import com.pets.service.AdminLoginService;
import com.pets.util.UseBaiDuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author
 */
@Service
public class AdminLoginServiceImpl implements AdminLoginService {
    @Autowired
    private AdminMapper adminDao;
    @Override
    public Admin adminFaceLogin(HttpServletRequest request) {
        Admin admin = new Admin();
        //获取传入的base64位的人脸图片
        String imgStr = request.getParameter("image");
        if (null != imgStr) {
            imgStr = imgStr.substring(imgStr.indexOf(",") + 1);
        }
        String result = UseBaiDuUtil.search(imgStr);
        System.out.println(result);
        //FaceMsg faceMsg = JSON.parseObject(result,FaceMsg.class);
        //String result = SearchFace.search(imgStr);
        //将result转成JSONObject格式
        JSONObject jo = JSONObject.parseObject(result);
        //error_msg 百度云接口的东西
        String error_msg = jo.getString("error_msg");
        // 如果SearchFace操作成功
        if (error_msg.equals("SUCCESS")) {
            String str = jo.getString("result");
            JSONObject rjo = JSONObject.parseObject(str);
            // 获取result结果中的user_list数组
            JSONArray jsonArray = rjo.getJSONArray("user_list");
            //将JSONArray数组转为list集合
            List<String> list = new ArrayList<>();
            if (jsonArray != null) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    list.add(jsonArray.getString(i));
                }
            }
            // 获取user_list中的值
            JSONObject ujo = JSONObject.parseObject(list.get(0));
            Double score = ujo.getDouble("score");
            String user_id = ujo.getString("user_id");
            String[] arr = user_id.split("_");
            // 根据获取userId
            int id = Integer.parseInt(arr[1]);

            if (score > 90) {
                // 根据id查询用户的信息
                admin= this.getAdminById(id);
            } else {
                admin=null;
            }
        } else {
           admin=null;
        }
        return admin;
    }

    @Override
    public Boolean adminFaceEntry(HttpServletRequest request, String adminId) {
        //获取传入的base64位的人脸图片
        String imgStr = request.getParameter("image");
        if (null != imgStr) {
            imgStr = imgStr.substring(imgStr.indexOf(",") + 1);
        }
        Boolean flag = UseBaiDuUtil.add(imgStr, "group0001", "2");

        return flag;
    }

    @Override
    public Admin getAdminById(int adminId) {
        return adminDao.selectByPrimaryKey(adminId);
    }

    @Override
    public Admin doLogin(Admin admin) {
        Admin admin1 = adminDao.selectByAdminNameAndPwd(admin.getAdminname(),admin.getAdminpwd());

        return admin1;
    }
}
